/*
 * Windy - Level Editor for Isometric Tile-based Games
 * Copyright (C) 2009 Ricardo J. Ricken (Darkl0rd)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef WINDY_TILEPANEL_HPP
#define WINDY_TILEPANEL_HPP

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include <wx/combobox.h>
#include <wx/tglbtn.h> 
#include <wx/dcbuffer.h>

#include "windy_document.hpp"
#include "windy_tileset.hpp"

class TilePanel : public wxPanel
{
public:
   TilePanel(wxWindow *parent, wxWindowID id, wxImageList &icons, const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize):
   wxPanel(parent, id, pos, size, wxTAB_TRAVERSAL | wxSIMPLE_BORDER | wxNO_FULL_REPAINT_ON_RESIZE | wxCLIP_CHILDREN),
   selected(0)
   {
      SetBackgroundColour(wxColor(0xcccccc));
      //SetBackgroundStyle(wxBG_STYLE_CUSTOM);

      wxBoxSizer *tlsetpick_sz = new wxBoxSizer(wxHORIZONTAL);
      wxComboBox *tilesetcb = new wxComboBox(this, WdyTilePanel_TlsetBox, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, 0, wxCB_READONLY | wxCB_SORT);
      wxBitmapButton *tlsetbrowse = new wxBitmapButton(this, WdyTilePanel_Add, icons.GetBitmap(WindyBmp_Add));
      tlsetpick_sz->Add(tilesetcb, wxSizerFlags(1).Expand().Border(wxTOP, 1));
      tlsetpick_sz->Add(tlsetbrowse, wxSizerFlags().Border(wxLEFT, 5));

      tiles_sz = new wxGridSizer(3, 5, 3);

      wxBoxSizer *global_sz = new wxBoxSizer(wxVERTICAL);
      global_sz->AddSpacer(5);
      global_sz->Add(tlsetpick_sz, wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT, 5));
      global_sz->AddSpacer(10);
      global_sz->Add(tiles_sz, wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT, 5));
      global_sz->SetMinSize(150, 100);

      SetSizer(global_sz);
   }

   void OnPaint (wxPaintEvent &event)
   {
      wxAutoBufferedPaintDC dc(this);
      wxSize sz = GetClientSize();

      dc.SetBackground(*wxTheBrushList->FindOrCreateBrush(wxColor(0xcccccc)));
      dc.Clear();

      dc.SetPen(*wxGREY_PEN);
      dc.DrawLine(sz.x - 1, 0, sz.x - 1, sz.y);
   }

   void  OnEraseBackground (wxEraseEvent &event) { }

   void changeTileset (WindyTileset &ts)
   {
      if (!selected || (selected && selected->name != ts.name))
      {
         selected = &ts;

         tiles_sz->Clear(true);

         for (int i = 0; i < ts.numTiles(); i++)
         {
            wxBitmapToggleButton *btn = new wxBitmapToggleButton(this, WdyTilePanel_TileBase + i, ts.getTile(i));
            tiles_sz->Add(btn, wxSizerFlags().Center());
         }

         GetParent()->Layout();
         GetParent()->Refresh();
         GetParent()->Update();
      }
   }

private:
   WindyTileset *selected;
   wxGridSizer *tiles_sz;

   DECLARE_EVENT_TABLE();
};

BEGIN_EVENT_TABLE(TilePanel, wxPanel)
   //EVT_PAINT(TilePanel::OnPaint)
   //EVT_ERASE_BACKGROUND(TilePanel::OnEraseBackground)
END_EVENT_TABLE()

#endif //~WINDY_TILEPANEL_HPP